#include <errno.h>
#include "process.h"
#include "logging.h"

namespace asce
{
	Process::Process()
	{
		pid_ = -1;
	}

	Process::~Process()
	{
	}

	bool Process::Creat(const std::string& file_path, char* argv[])
	{
		if (valid())
		{
			Logging::Instance().Write(LOG_ERROR, "Process::Creat Valid process.");
			return false;
		}

		pid_ = fork();
		if (-1 == pid_)
		{
			Logging::Instance().Write(LOG_ERROR, "Process::Creat Error:%s:%d.", strerror(errno), errno);
			return false;
		}

		if (0 == pid_)
		{
			if (-1 == execv(file_path.c_str(), argv))
			{
				exit(0);
			}
		}

		return true;
	}

	bool Process::Wait()
	{
		if (!valid())
		{
			Logging::Instance().Write(LOG_ERROR, "Process::Wait Invalid process.");
			return false;
		}

		return pid_ == waitpid(pid_, NULL, 0);
	}
}
